package com.thesis.manage.service.impl;

import com.github.crab2died.ExcelUtils;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.mongodb.impl.AbstractMongoServiceImpl;
import com.thesis.manage.domain.*;
import com.thesis.manage.service.*;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.RoleEnum;
import com.thesis.manage.service.module.excel.AnswerGroupExcel;
import com.thesis.manage.service.module.vo.AnswerGroupVO;
import com.thesis.manage.service.module.vo.StudentVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/4/4
 */
@Service
public class AnswerGroupServiceImpl extends AbstractMongoServiceImpl<AnswerGroup> implements AnswerGroupService {

    @Resource
    private TeacherService teacherService;
    @Resource
    private TeacherRoleService teacherRoleService;
    @Resource
    private GradeService gradeService;

    @Resource
    private StudentTeacherService studentTeacherService;

    @Override
    protected Class<AnswerGroup> getEntityClass() {
        return AnswerGroup.class;
    }

    @Override
    public void importGroupInfo(String name, String collegeName, MultipartFile file) {
        //导入时存在数据则删除全部数据
        List<AnswerGroup> answerGroups = this.groupInfo(collegeName);
        if (CollectionUtils.isNotEmpty(answerGroups)) {
            for (AnswerGroup group : answerGroups) {
                this.deleteById(group.getId());
            }
        }
        try {
            List<AnswerGroupExcel> groupExcels = ExcelUtils.getInstance().readExcel2Objects(file.getInputStream(), AnswerGroupExcel.class);
            if (CollectionUtils.isNotEmpty(groupExcels)) {
                AnswerGroup answerGroup;
                for (AnswerGroupExcel groupExcel : groupExcels) {
                    answerGroup = new AnswerGroup();
                    answerGroup.setCollegeName(collegeName);
                    answerGroup.setIndex(groupExcel.getIndex());
                    answerGroup.setTeacherList(groupExcel.getTeacherList());
                    answerGroup.setUploadPerson(name);
                    this.insert(answerGroup);
                }
            }
        } catch (Exception e) {
            throw new CommonException("import group info failed", "导入分组信息失败");
        }
    }

    @Override
    public List<AnswerGroup> groupInfo(String collegeName) {
        AnswerGroup answerGroup = new AnswerGroup();
        answerGroup.setCollegeName(collegeName);
        List<AnswerGroup> answerGroups = this.find(answerGroup);
        if (CollectionUtils.isNotEmpty(answerGroups)) {
            for (AnswerGroup ag : answerGroups) {
                String[] arr = ag.getTeacherList().split("、");
                if (ArrayUtils.isNotEmpty(arr)) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String teacherNumber : arr) {
                        Teacher teacher = this.teacherService.findByTeacherNumber(teacherNumber);
                        if (teacher != null) {
                            stringBuilder.append(teacher.getName()).append("  ");
                        }
                    }
                    ag.setTeacherList(stringBuilder.toString());
                }
            }
        }
        return answerGroups;
    }

    @Override
    public void addPerson(Long id, String collegeName, String teacherNumber) {
        Assert.notNull(id, ErrorMessage.PARAMS_IS_NULL);
        Teacher teacher = this.teacherService.findByTeacherNumber(teacherNumber);
        if (teacher == null) {
            throw new CommonException("teacher not exist", "教师不存在");
        }
        AnswerGroup group = this.findById(id);
        if (group.getPersonId() != null) {
            TeacherRole teacherRole = new TeacherRole();
            teacherRole.setTeacherId(group.getPersonId());
            teacherRole.setRoleId(RoleEnum.WRITER_PERSON.getValue().longValue());
            TeacherRole one = this.teacherRoleService.findByOne(teacherRole);
            if (one != null) {
                this.teacherRoleService.delete(one.getId());
            }
        }
        AnswerGroup answerGroup = new AnswerGroup();
        answerGroup.setId(id);
        answerGroup.setPerson(teacher.getName());
        answerGroup.setPersonId(teacher.getId());
        this.update(answerGroup);
        TeacherRole teacherRole = new TeacherRole();
        teacherRole.setRoleId(RoleEnum.WRITER_PERSON.getValue().longValue());
        teacherRole.setTeacherId(teacher.getId());
        teacherRole.setCollegeName(collegeName);
        this.teacherRoleService.save(teacherRole);
    }

    @Override
    public List<AnswerGroupVO> findByPerson(Long id) {
        Assert.notNull(id, ErrorMessage.PARAMS_IS_NULL);
        List<AnswerGroupVO> list = new ArrayList<>();
        AnswerGroup answerGroup = new AnswerGroup();
        answerGroup.setPersonId(id);
        AnswerGroup group = this.findByOne(answerGroup);
        if (group != null) {
            String teacherList = group.getTeacherList();
            if (StringUtils.isNotBlank(teacherList)) {
                String[] arr = teacherList.split("、");
                StudentTeacher studentTeacher;
                for (String person : arr) {
                    studentTeacher = new StudentTeacher();
                    studentTeacher.setTeacherNumber(person);
                    List<StudentVO> studentVOS = this.studentTeacherService.findAllByTeacherId(studentTeacher);
                    if (CollectionUtils.isNotEmpty(studentVOS)) {
                        AnswerGroupVO answerGroupVO;
                        for (StudentVO studentVO : studentVOS) {
                            answerGroupVO = new AnswerGroupVO();
                            answerGroupVO.setIndex(group.getIndex());
                            answerGroupVO.setCollegeName(group.getCollegeName());
                            answerGroupVO.setStudentId(studentVO.getStudentNumber());
                            answerGroupVO.setStudentName(studentVO.getName());
                            answerGroupVO.setStudentNumber(studentVO.getStudentNumber());
                            Grade grade = this.gradeService.findByStudentId(studentVO.getStudentNumber());
                            if (grade != null) {
                                answerGroupVO.setAnswerGrade(grade.getAnswerGrade());
                                answerGroupVO.setReviewGrade(grade.getReviewGrade());
                            }
                            list.add(answerGroupVO);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public void addAnswerRecord(Long id, String content, String url) {
        AnswerGroup answerGroup = new AnswerGroup();
        answerGroup.setPersonId(id);
        AnswerGroup one = this.findByOne(answerGroup);
        if (one == null) {
            throw new CommonException("you isn`t recorder", "你不是记录员,不能添加答辩过程文档");
        }
        answerGroup = new AnswerGroup();
        answerGroup.setId(one.getId());
        answerGroup.setContent(content);
        answerGroup.setUrl(url);
        this.update(answerGroup);
    }

    @Override
    public AnswerGroup showAnswerRecord(Long id) {
        AnswerGroup answerGroup = new AnswerGroup();
        answerGroup.setPersonId(id);
        return this.findByOne(answerGroup);
    }
}
